/* 
 *  Copyright 2012 CodeMagi, Inc.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.codemagi.database;

import com.codemagi.util.*;
import java.util.*;
import org.apache.log4j.*;

/**
 * This bean abstracts JDBC Database access using a JNDI DataSource.
 *
 * This class differs from DataSourceBean in that it caches results in a GridList instead of a 2D String array.
 * 
 * @version 1.0
 * @author August Detlefsen for CodeMagi, Inc.
 */
public class DataSourceBean2 extends DataSourceBean {
	
    // display debug output
    Logger log = Logger.getLogger(this.getClass());
	
    // The result set of the db query represented in a 2 dimensional array. 
    protected GridList twoDArray = null;
	

    /**
     * Constructor
     * 
     * @param resourceName  The JNDI resource to connect to
     */
    public DataSourceBean2( String resourceName ) {
	super(resourceName);
    }


    /**
     * Returns the result set of the current query, as a com.codemagi.util.GridList
     */
    public GridList getResults() {
	return twoDArray;
    }


    /**
     * Get the specified item (row, col) from the 2D array representation
     * of the result set.
     *
     * @param row       The row of the item to get.
     * @param col       The column of the item to get.
     *
     * @return String   The item if row and col are legal values, null or "" otherwise
     */    
    public String getItem( int row, int col ) {
    	
	Object retObj = null;
        String ret    = null;

        if ( row < getNumRows() && col < getNumCols() && twoDArray != null ) {   //numCols is 1 based, twoDArray is zero based
	    retObj = twoDArray.getItem(row, col);
	    if (retObj != null) ret = retObj.toString();
        }
        
	//if returnNulls is false, convert null values to ""
	if ( ret == null && !returnNulls) ret = "";
	
        return ret;
    }


    /**
     * Creates a cache of the JDBC result set.     
     *
     * @return String[][]  The 2 dimensional List of Strings representing the result set.
     */
    protected void create2DArray() {
		
	//make sure we set the class-level current row back 
	setCurrentRow(-1);
	
        int increment = 10;
        int currentRow = 0;
        
	//get the number of columns inthe result set
	setNumCols();
        int numCols = getNumCols();

	//estimate the number of rows in the result set
	int estNumRows = estimateNumRows();

	GridList ret = new GridList(estNumRows);   //using default ArrayList

	//set the column names into the GridList
	log.debug("SETTING COLUMN NAMES WITH: " + columnNames);
	ret.setColumnNames(columnNames);

        while ( nextResSetRow() ) {
        	
	    ArrayList row = new ArrayList(numCols);
	    
	    // copy the current row into the return array
	    for ( int col = 0; col < numCols; col++ ) {
		row.add( getRowSetItem(col+1) );
	    }

	    ret.addRow(row);

            currentRow++;            
        }
        
        numRows = currentRow;
        
        twoDArray = ret;
    }

}




